import storage from 'store'
import expirePlugin from 'store/plugins/expire'
import axios from "axios";
import {userApi} from "@/api/user";

storage.addPlugin(expirePlugin)
const user = {
  state: {
    uuid: '',
    token: '',
    name: '',
    avatar: '',
    roles: {
      admin: "管理员",
      auditor :"审核员",
      expert:"专家",
      user: "用户",
      ban: "封禁用户"
    },
    genders:['未知','男','女'],
    info: {}
  },

  mutations: {
    SET_UUID: (state, uuid) => {
      state.uuid = uuid
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // getUUID
    getUUIDputState({commit}) {
      return new Promise((resolve, reject) => {
        axios.get(userApi.getUUID).then(res => {
          commit('SET_UUID', res.data.data)
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    //setInfo
    getINFOputState({commit}){
      return new Promise((resolve, reject) => {
        axios.get(userApi.getInfo).then(res => {
          commit('SET_INFO', res.data.data)
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    }
  },
  getters:{
    getInfo(state){
      return state.info
    }
  }
}

export default user
